    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameCalculoEnvolvente.java
 *
 * Created on Dec 7, 2011, 2:46:56 PM
 */
package ar.com.wholeconsulting.pacc.gui.modelos.calculos;

import ar.com.wholeconsulting.pacc.gui.utils.table.TableShowFieldsModel;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.generic.GenericEditorItems;
import ar.com.wholeconsulting.pacc.gui.utils.treetable.generic.GenericTreeTableModel;
import ar.com.wholeconsulting.pacc.model.Acero;
import ar.com.wholeconsulting.pacc.model.Envolvente;
import ar.com.wholeconsulting.pacc.model.MetodoSoldadura;
import ar.com.wholeconsulting.pacc.model.Modelo;
import ar.com.wholeconsulting.pacc.model.RadiografiaSoldadura;
import ar.com.wholeconsulting.pacc.model.Soldadura;
import ar.com.wholeconsulting.pacc.persistance.JPASession;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

/**
 *
 * @author pable
 */
public class FrameCalculoEnvolvente extends javax.swing.JDialog {

    private Modelo currentModelo;
    private Envolvente currentEnvolvente;
    private boolean isNew = false;

    /** Creates new form FrameCalculoEnvolvente */
    public FrameCalculoEnvolvente(JDialog frame,Modelo modelo) {
        super(frame, true);
        isNew = true;
        this.currentModelo = modelo;
        this.currentEnvolvente = new Envolvente();
        initComponents();
        initComponentsView();
    }

    public FrameCalculoEnvolvente(JDialog frame,Modelo modelo, Envolvente env) {
        super(frame, true);
        this.currentModelo = modelo;
        this.currentEnvolvente = env;
        initComponents();
        initComponentsView();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonCancel = new javax.swing.JButton();
        buttonSave = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeTableAtributos = new org.jdesktop.swingx.JXTreeTable();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableValoresCalculados = new javax.swing.JTable();
        jLabel23 = new javax.swing.JLabel();
        textEspesorNominal1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        textNombreModelo = new javax.swing.JTextField();

        setTitle("Calculo de Envolvente s/ASME VIII-DIV.1");
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setResizable(false);

        buttonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/trash_16x16.gif"))); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        buttonSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/aspneticons_v1.0_Nov2006/save_16x16.gif"))); // NOI18N
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(treeTableAtributos);

        jTabbedPane1.addTab("Atributos", jScrollPane1);

        jScrollPane2.setViewportView(tableValoresCalculados);

        jLabel23.setText("Espesor Nominal");

        textEspesorNominal1.setText("0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel23, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(textEspesorNominal1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel23)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textEspesorNominal1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Valores Calculados", jPanel1);

        jLabel2.setText("Modelo:");

        textNombreModelo.setEditable(false);
        textNombreModelo.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textNombreModelo, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textNombreModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 444, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buttonCancel)
                    .addComponent(buttonSave))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-491)/2, (screenSize.height-566)/2, 491, 566);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
        if(isNew){
            currentModelo.getElementos().add(currentEnvolvente);
        }
        currentEnvolvente.setEspesorNominal(Double.parseDouble(textEspesorNominal1.getText()));
        this.setVisible(false);
    }//GEN-LAST:event_buttonSaveActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        if(!isNew){
            JPASession.getInstance().getManager().refresh(currentModelo);
        }
        this.setVisible(false);
    }//GEN-LAST:event_buttonCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonSave;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tableValoresCalculados;
    private javax.swing.JTextField textEspesorNominal1;
    private javax.swing.JTextField textNombreModelo;
    private org.jdesktop.swingx.JXTreeTable treeTableAtributos;
    // End of variables declaration//GEN-END:variables

    private void initComponentsView() {
        textNombreModelo.setText(currentModelo.getNombre());
        this.setTitle(currentModelo.getFabricante() + " - " + currentModelo.getNombre());
        textEspesorNominal1.setText(String.valueOf(currentEnvolvente.getEspesorNominal()));
        initTreeAtributos();
        initTableCalculos();
    }

    private void initTreeAtributos() {
        GenericTreeTableModel model = new GenericTreeTableModel(treeTableAtributos);
        List<GenericEditorItems> list = new LinkedList<GenericEditorItems>();
        list.add(new GenericEditorItems("Dimensiones",
                new String[]{"Diametro Externo", "Longitud", "Eficiencia de Junta", "Tipo de Soldadura", "Metodo de Soldadura", "Radiografiado"},
                new String[]{"diametro", "longitud", "eficienciaDeJunta", "tipoSoldadura", "metodoSoldadura", "radiografia"},
                new DefaultCellEditor[]{null, null, null,
                    new DefaultCellEditor(new JComboBox(Soldadura.values())),
                    new DefaultCellEditor(new JComboBox(MetodoSoldadura.values())),
                    new DefaultCellEditor(new JComboBox(RadiografiaSoldadura.values()))}, currentEnvolvente));

        list.add(new GenericEditorItems("Consideraciones del Material",
                new String[]{"Tension Admisible", "Material"},
                new String[]{"tensionAdmisible", "acero"},
                new DefaultCellEditor[]{null,
                    new DefaultCellEditor(new JComboBox(JPASession.getInstance().getResultListNamedQuery(Acero.FIND_ALL).toArray()))}, currentEnvolvente));
        model.initTreeTable(list);
        treeTableAtributos.updateUI();
    }

    private void initTableCalculos() {
        TableModel model = new TableShowFieldsModel(new Object[][]{
                    {"Espesor Minimo Requerido", null},
                    {"Desarrollo", null},
                    {"Long Desarrollo", null},
                    {"Capacidad ", null},
                    {"Peso ", null}
                }, tableValoresCalculados, new TableCellEditor[5],
                new String[]{"espesorDeCalculo","desarrollo","desarrollo","volumen","peso"}, currentEnvolvente);
        //tableValoresCalculados.setModel(model);
        tableValoresCalculados.updateUI();
    }
}
